package com.cetsk.android.talkorigins;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

public class ClaimViewActivity extends FragmentActivity {
	WebView webview;
	private ProgressDialog dialog;
	String html = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		dialog = ProgressDialog.show(this,"Loading", "Loading claim...",true);
		new Thread() {
			public void run() {
				try{
					Claim claim = (Claim) getIntent().getSerializableExtra("claim");
					setTitle(claim.getName());
					HTMLParse hp = new HTMLParse(getApplicationContext());
					html = hp.getHtml("http://www.talkorigins.org/indexcc/"+claim.getUrl());
					html = html.replaceAll("\\.\\./", "http://talkorigins.org/indexcc/");
				} catch (Exception e) { }
				handler.sendEmptyMessage(0);
				dialog.dismiss(); 
			}
		}.start();
		super.onCreate(savedInstanceState);
	}
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			setContentView(R.layout.claim_view);
			webview = (WebView) findViewById(R.id.webview);
			if(html != null && html.length() > 0){
				webview.loadData(html, "text/html", "utf-8");
			} else {
				Toast.makeText(getApplicationContext(),"There was a problem fetching HTML.", Toast.LENGTH_SHORT).show();
				Log.i("HTML_ERROR", "Could not Parse");
			}
		}
	};
}